package com.changgou.goods.service.impl;

import com.changgou.goods.dao.BrandMapper;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;

    @Override
    public List<Brand> findAll() {
        List<Brand> brandList = brandMapper.selectAll();
        return brandList;
    }

    @Override
    public Brand findById(Integer id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        return brand;
    }

    @Override
    public void add(Brand brand) {
        brandMapper.insertSelective(brand);
    }

    @Override
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    @Override
    public void deleteById(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Brand> search(Map<String,String> map) {
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if (map != null && map.size() > 0) {
            if (StringUtils.isNotEmpty(map.get("name")) && !map.get("name").equals("")) {
                criteria.andLike("name","%"+map.get("name")+"%");
            }
        }
        if (map != null && map.size() > 0) {
            if (StringUtils.isNotEmpty(map.get("letter")) && !map.get("letter").equals("")) {
                criteria.andEqualTo("letter",map.get("name"));
            }
        }
        List<Brand> brandList = brandMapper.selectByExample(example);
        return brandList;
    }

    @Override
    public Page<Brand> searchPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<Brand> brandList = (Page<Brand>) brandMapper.selectAll();
        return brandList;
    }

    @Override
    public Page<Brand> searchPageByLike(int pageNum, int pageSize, Map<String, String> map) {
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        if (map != null && map.size() > 0) {
            if (StringUtils.isNotEmpty(map.get("name")) && !map.get("name").equals("")) {
                criteria.andLike("name","%"+map.get("name")+"%");
            }
        }
        if (map != null && map.size() > 0) {
            if (StringUtils.isNotEmpty(map.get("letter")) && !map.get("letter").equals("")) {
                criteria.andEqualTo("letter",map.get("name"));
            }
        }
        PageHelper.startPage(pageNum,pageSize);
        Page<Brand> brandList = (Page<Brand>) brandMapper.selectByExample(example);
        return brandList;
    }
}
